import { mockMenuData, mockNavbarConfigs } from './mock.js'

// 导出移动端底部菜单数据，用于直接获取


/**
 * 功能菜单服务类
 */
class FuncMenuService {
    /**
     * 获取功能菜单列表
     * @returns {Promise<Object>} 菜单数据
     */
    async getApplicationInfo () {
        try {
            await this.delay(100)
            return Promise.resolve(mockMenuData.data)
        } catch (error) {
            return Promise.reject(error)
        }
    }
    /**
     * 获取移动端底部菜单数据
     * @returns {Array} 移动端底部菜单数据
     */
    getMobileNavbarConfigs () {
        return mockNavbarConfigs.filter(f => f.IsEnable);
    }

    async getAllFunctions () {
        try {
            const applicationInfo = await this.getApplicationInfo()
            const allFunctions = []

            const extractFunctions = (childGroups) => {
                if (!Array.isArray(childGroups)) return

                childGroups.forEach(group => {
                    if (group.Functions && Array.isArray(group.Functions)) {
                        const functionsWithGroupInfo = group.Functions.map(func => ({
                            ...func,
                            GroupName: group.Name,
                            ApplicationID: applicationInfo.ApplicationID,
                            ApplicationName: applicationInfo.AppFullName
                        }))
                        allFunctions.push(...functionsWithGroupInfo)
                    }

                    if (group.ChildGroup && Array.isArray(group.ChildGroup)) {
                        extractFunctions(group.ChildGroup)
                    }
                })
            }

            if (applicationInfo.ChildGroup) {
                extractFunctions(applicationInfo.ChildGroup)
            }

            if (applicationInfo.Functions && Array.isArray(applicationInfo.Functions)) {
                allFunctions.push(...applicationInfo.Functions)
            }

            return Promise.resolve(allFunctions)
        } catch (error) {
            return Promise.reject(error)
        }
    }


    /**
     * 搜索功能菜单
     * @param {string} keyword - 搜索关键字
     * @returns {Promise<Array>} 搜索结果，按ApplicationName分组
     */
    async searchFunctions (keyword) {
        try {
            if (!keyword || !keyword.trim()) {
                return Promise.resolve([])
            }

            const allFunctions = await this.getAllFunctions()
            const query = keyword.toLowerCase().trim()

            const filteredFunctions = allFunctions.filter(func =>
                func.Name.toLowerCase().includes(query)
            )

            // 按ApplicationName分组
            const groupedResults = []
            const applicationMap = new Map()

            filteredFunctions.forEach(func => {
                const appId = func.ApplicationID
                const appName = func.ApplicationName

                if (!applicationMap.has(appId)) {
                    const appGroup = {
                        ApplicationID: appId,
                        ApplicationName: appName,
                        FunctionList: []
                    }
                    applicationMap.set(appId, appGroup)
                    groupedResults.push(appGroup)
                }

                applicationMap.get(appId).FunctionList.push(func)
            })

            return Promise.resolve(groupedResults)
        } catch (error) {
            return Promise.reject(error)
        }
    }

    /**
     * 延迟函数，模拟异步请求
     * @param {number} ms - 延迟毫秒数
     * @returns {Promise}
     */
    delay (ms) {
        return new Promise(resolve => setTimeout(resolve, ms))
    }



}

// 创建服务实例
const funcMenuService = new FuncMenuService()

// 导出服务实例和类
export default funcMenuService
export { FuncMenuService }

export const setBrowserTitle = (title) => {
    document.title = title
}
export const getApplicationInfo = () => funcMenuService.getApplicationInfo()
export const getAllFunctions = () => funcMenuService.getAllFunctions()
export const getMobileNavbarConfigs = () => funcMenuService.getMobileNavbarConfigs()
export const searchFunctions = (keyword) => funcMenuService.searchFunctions(keyword)
export { navigateToMenu, navigateInNewTab, navigateToMenuByConfig } from './funcOpen.js'